package com.zhu.aliyunService.controller;

import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zhu.aliyunService.service.videoService;
import com.zhu.commons.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

@RestController
@RequestMapping("admin/aliyun/video")
@Api(tags = "阿里云视频上传模块")
//@CrossOrigin
public class VideoController {
    @Autowired
    private videoService  videoService;

    @PostMapping("/upload")
    @ApiOperation(value = "阿里云视频上传")
    public R upload(@RequestParam("file") MultipartFile file) throws IOException {
            InputStream inputStream = null;
            inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();
            String s = videoService.uploadVideo(inputStream, originalFilename);
            return R.ok().data("videoId",s).message("文件上传成功");
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(value = "删除视频")
    public R delete(@PathVariable("id")String id) throws Exception {
        if(videoService.delete(id)){
            return R.ok().message("视频删除成功！");
        }
        return R.error().message("视频删除失败");
    }
    @DeleteMapping("/deleteList")
    @ApiOperation(value = "批量删除视频")
    public R deleteVideoList(@RequestBody List<String> IdList) throws Exception {
        if(videoService.deleteList(IdList)){
            return R.ok().message("视频批量删除成功！");
        }
        return R.error().message("视频批量删除失败");
    }

    @PostMapping("/getAuthRization/{id}")
    @ApiOperation("获得视频凭证")
    public R getPlayAuth(@PathVariable(value = "id") String id) throws ClientException {
      String auth=  videoService.getPlayAuth(id);
      if(auth!=null){
          return R.ok().data("auth",auth);
      }
      return R.error().message("获取视频凭证失败！");
    }
}
